class Solution {
public:
    int binarysearch(vector<int>& nums, int l, int r, int target) {
        int i = l, j = r;
        while (i <= j) {
            int mid = (i + j) / 2;
            if (nums[mid] > target)
                j = mid - 1;
            else if (nums[mid] < target)
                i = mid + 1;
            else
                return mid;
        }
        return -1;
    }
    int search(vector<int>& nums, int target) {
        int i = 0;
        int j = nums.size() - 1;
        if (nums[i] < nums[j])
            return binarysearch(nums, i, j, target);
        else {
            while (i <= j) {
                int mid = (i + j) / 2;
                if (nums[mid] <= nums[nums.size()-1])
                    j = mid - 1;
                else
                    i = mid + 1;
            }
            int l = 0;
            int r = nums.size() - 1;
            if (target <= nums[r])
                return binarysearch(nums, i, r, target);
            else
                return binarysearch(nums, l, j, target);
        }
    }
};